package org.wanda.pv.controller.hy;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.wanda.pv.framework.base.Result;
import org.wanda.pv.model.hy.HyTree;

import java.util.List;

/**
 * @author lxy
 * @date 2018/11/22 14:33
 */

@RestController
@RequestMapping("/hy/tree")
public class HyTreeController {
    @Autowired
    Dao dao;
    private final static Logger log = LoggerFactory.getLogger(HyTreeController.class);

    @RequestMapping("/getTree")
    public Object getTree(){
        HyTree parent=null;
        try {
            // 获取最顶层的数据
            parent = dao.fetch(HyTree.class, Cnd.where("parentId", "=", 39).and("delFlag","=","0"));
            getTree_(parent);
        }catch (Exception e){
            log.error(e.getMessage());
        }

        return Result.success("success",parent);


    }

    private void getTree_(HyTree parent) {

        List<HyTree> parents = dao.query(HyTree.class, Cnd.where("parentId", "=", parent.getId()).and("delFlag","=","0"));
        parent.setChildren(parents);
        parents.forEach(item->{
            List<HyTree> parents1 = dao.query(HyTree.class, Cnd.where("parentId", "=", parent.getId()).and("delFlag","=","0"));
            parent.setChildren(parents1);
        });



    }
}
